using System;
using System.Collections.Generic;
using System.Text;
using CodeStory.Gallery.ObjectEntity;
using Activa.CoolStorage;

namespace CodeStory.Gallery.ObjectBusiness
{
	public sealed class GalleryMainBusiness
	{
		private GalleryMainBusiness() {

		}

		public static void InsertOrUpdate(string galleryid, Guid userid, string galleryname, string description) {
			GalleryMainObject obj;
			if (string.IsNullOrEmpty(galleryid)) {
				obj = GalleryMainObject.New();
			} else {
				Guid id = new Guid(galleryid);
				obj = GalleryMainObject.Read(id);
			}
			obj.UserID = userid;
			obj.GalleryName = galleryname;
			obj.Description = description;
			obj.Save();
		}

		public static GalleryMainObject Read(string galleryid) {
			GalleryMainObject obj;
			if (string.IsNullOrEmpty(galleryid)) return null;
			Guid id = new Guid(galleryid);
			obj = GalleryMainObject.Read(id);
			return obj;
		}

		public static bool GalleryHasCreated(Guid userid) {
			GalleryMainObject obj = GalleryMainObject.ReadFirst("UserID=@UserID","@UserID",userid);
			return obj != null;
		}

		public static IEnumerable<GalleryMainObject> ListPage(int pagesize, int pageindex, out int recordcount) {
			CSList<GalleryMainObject> list = GalleryMainObject.List().Range(pagesize * pageindex, pagesize);
			recordcount = CSDatabase.GetScalar<int>("SELECT COUNT(GalleryID) FROM Gallery_Main");
			return list;
		}
	}
}
